/*
 * ReportViewerPanel.java
 *
 * Created on 16 de Mar�o de 2003, 23:57
 */
package saci.reptil;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import saci.reptil.writer.Exporter;
import saci.reptil.writer.UserCancelException;

/**
 *
 * @author  saci
 */
public class ReportViewerPanel extends javax.swing.JPanel {

    final static long serialVersionUID = 14236775475312083L;
    private ReportManager reportManager;
    private Frame parent;

    public ReportViewerPanel(ReportManager rm) {
        this(rm, "Close");
    }

    public ReportViewerPanel(ReportManager rm, String closeCaption) {
        this(rm, null, closeCaption);
    }

    public ReportViewerPanel(ReportManager rm, Frame parent, String closeCaption) {
        initComponents();

        this.reportManager = rm;
        lblNumberOfPages.setText(String.valueOf(rm.getNumberOfPages()));

        createPageView(rm.getPageAsImage(0), 100);

        txtPage.addKeyListener(new saci.reptil.util.Util.NumericListener());
        cmbZoom.addKeyListener(new saci.reptil.util.Util.NumericListener());

        this.parent = parent;

        btnClose.setVisible(parent != null);
        btnClose.setText(closeCaption);
    }

    private void createPageView(final Image i, double zoom) {
        int w;
        int h;

        if (reportManager.getOrientation() == ReportManager.LANDSCAPE) {
            reportManager.getPaper().setSize(reportManager.getPaper().getHeight(), reportManager.getPaper().getWidth());
        }

        w = (int) reportManager.getPaper().getWidth();
        h = (int) reportManager.getPaper().getHeight();

        if (zoom != 100) {
            if (zoom > 100) {
                zoom -= 100;
                w += w * (zoom / 100);
                h += h * (zoom / 100);
            } else {
                zoom = 100 - zoom;
                w -= w * (zoom / 100);
                h -= h * (zoom / 100);
            }
        }

        final int width = w;
        final int height = h;

        JPanel pPageView = new JPanel() {

            final static long serialVersionUID = 14236775475312074L;

            public void paint(Graphics g) {
                super.paint(g);

                int x = 0, y = 0;
                if (sReport.getSize().width > width) {
                    x = (sReport.getSize().width - width) / 2;
                }
                if (sReport.getSize().height > height) {
                    y = (sReport.getSize().height - height) / 2;
                }

                g.drawImage(i, x, y, width, height, null);

                // draw border ************************************************
                g.setColor(Color.black);
                g.drawRect(x, y, width, height);

                g.fillRect(x + width, y + 3, 3, height);
                g.fillRect(x + 3, y + height, width, 3);
            }
        };

        pPageView.setPreferredSize(new Dimension(width + 4, height + 4));
        pPageView.setSize(pPageView.getPreferredSize());

        sReport.setViewportView(pPageView);

        this.validate();

        if (reportManager.getOrientation() == ReportManager.LANDSCAPE) {
            reportManager.getPaper().setSize(reportManager.getPaper().getHeight(), reportManager.getPaper().getWidth());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        pButtons = new javax.swing.JPanel();
        btnPrior = new javax.swing.JButton();
        txtPage = new javax.swing.JTextField();
        btnNext = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        cmbZoom = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        btnSave = new javax.swing.JButton();
        jLabel111 = new javax.swing.JLabel();
        btnPrint = new javax.swing.JButton();
        jLabel1111 = new javax.swing.JLabel();
        btnClose = new javax.swing.JButton();
        sReport = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        lblNumberOfPagesC = new javax.swing.JLabel();
        lblNumberOfPages = new javax.swing.JLabel();

        setLayout(new java.awt.BorderLayout());

        pButtons.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        btnPrior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/saci/reptil/images/prior.gif")));
        btnPrior.setToolTipText("Prior");
        btnPrior.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPriorActionPerformed(evt);
            }
        });

        pButtons.add(btnPrior);

        txtPage.setColumns(3);
        txtPage.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtPage.setText("1");
        txtPage.addKeyListener(new java.awt.event.KeyAdapter() {

            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPageKeyPressed(evt);
            }
        });

        pButtons.add(txtPage);

        btnNext.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/saci/reptil/images/next.gif")));
        btnNext.setToolTipText("Next");
        btnNext.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });

        pButtons.add(btnNext);

        jLabel1.setPreferredSize(new java.awt.Dimension(10, 10));
        pButtons.add(jLabel1);

        cmbZoom.setEditable(true);
        cmbZoom.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"25", "50", "75", "100", "150", "200"}));
        cmbZoom.setSelectedIndex(3);
        cmbZoom.setPreferredSize(new java.awt.Dimension(70, 26));
        cmbZoom.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbZoomActionPerformed(evt);
            }
        });

        pButtons.add(cmbZoom);

        jLabel11.setPreferredSize(new java.awt.Dimension(10, 10));
        pButtons.add(jLabel11);

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/saci/reptil/images/save.gif")));
        btnSave.setToolTipText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        pButtons.add(btnSave);

        jLabel111.setPreferredSize(new java.awt.Dimension(10, 10));
        pButtons.add(jLabel111);

        btnPrint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/inf/saci/reptil/images/print.gif")));
        btnPrint.setToolTipText("Print");
        btnPrint.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrintActionPerformed(evt);
            }
        });

        pButtons.add(btnPrint);

        jLabel1111.setPreferredSize(new java.awt.Dimension(10, 10));
        pButtons.add(jLabel1111);

        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        pButtons.add(btnClose);

        add(pButtons, java.awt.BorderLayout.NORTH);

        sReport.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.LOWERED));
        add(sReport, java.awt.BorderLayout.CENTER);

        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        lblNumberOfPagesC.setText("Number of Pages:");
        jPanel1.add(lblNumberOfPagesC);

        lblNumberOfPages.setText("0");
        jPanel1.add(lblNumberOfPages);

        add(jPanel1, java.awt.BorderLayout.SOUTH);

    }//GEN-END:initComponents

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        try {
            new Exporter(reportManager).save();
        } catch (UserCancelException ece) {
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.toString());
            e.printStackTrace();
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        parent.dispose();
    }//GEN-LAST:event_btnCloseActionPerformed

    private void btnPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrintActionPerformed
        try {
            reportManager.print(true);
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e.toString(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnPrintActionPerformed

    private void btnPriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPriorActionPerformed
        int page = Integer.parseInt(txtPage.getText());

        if (page > 1) {
            txtPage.setText(String.valueOf(--page));
            cmbZoomActionPerformed(null);
        }
    }//GEN-LAST:event_btnPriorActionPerformed

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        int page = Integer.parseInt(txtPage.getText());

        if (page < reportManager.getNumberOfPages()) {
            txtPage.setText(String.valueOf(++page));
            cmbZoomActionPerformed(null);
        }
    }//GEN-LAST:event_btnNextActionPerformed

    private void txtPageKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPageKeyPressed
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
            cmbZoomActionPerformed(null);
        }
    }//GEN-LAST:event_txtPageKeyPressed

    private void cmbZoomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbZoomActionPerformed
        createPageView(reportManager.getPageAsImage(Integer.parseInt(txtPage.getText()) - 1), Integer.parseInt(cmbZoom.getSelectedItem().toString()));
    }//GEN-LAST:event_cmbZoomActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnSave;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblNumberOfPagesC;
    private javax.swing.JButton btnPrint;
    private javax.swing.JPanel pButtons;
    private javax.swing.JButton btnNext;
    private javax.swing.JComboBox cmbZoom;
    private javax.swing.JLabel jLabel1111;
    private javax.swing.JLabel jLabel111;
    private javax.swing.JTextField txtPage;
    private javax.swing.JLabel lblNumberOfPages;
    private javax.swing.JButton btnPrior;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JScrollPane sReport;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
}
